package controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import entity.t_complaont;
import entity.t_transaction;
import entity.t_travel;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import service.TravelAgencyService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/travelAgency")
public class TravelAgencyController {

    @Resource
    TravelAgencyService travelAgencyService;


    //查询 基本情况页面
    @RequestMapping("/basic")
    public String basic(Model model, @RequestParam(required = false)String tt_travel_Name,
                        @RequestParam(required = false)String tt_travel_Type, HttpSession session){

        if ("0".equals(tt_travel_Type)){
            tt_travel_Type=null;
        }
        List<t_travel> travelList=travelAgencyService.getListAll(tt_travel_Name,tt_travel_Type);
        session.setAttribute("travelList",travelList);

        model.addAttribute("tt_travel_Name",tt_travel_Name);
        model.addAttribute("tt_travel_Type",tt_travel_Type);

        return "/travelAgency/basic";
    }

    //查询 线路情况
    @RequestMapping("/routes")
    public String routes(Model model,@RequestParam(required = false)String tt_travel_Name,
                         @RequestParam(required = false)String tt_travel_Open,@RequestParam(required = false)String tt_travel_Createtime,
                         @RequestParam(required = false)String tt_travel_Start,@RequestParam(required = false)String tt_travel_End){
        if ("0".equals(tt_travel_Open)){
            tt_travel_Open=null;
        }
        List<t_travel> travelList=travelAgencyService.getListRoutes(tt_travel_Name,tt_travel_Open,tt_travel_Createtime,tt_travel_Start,tt_travel_End);
        model.addAttribute("travelList",travelList);
        model.addAttribute("tt_travel_Name",tt_travel_Name);
        model.addAttribute("tt_travel_Type",tt_travel_Open);
        model.addAttribute("tt_travel_Type",tt_travel_Createtime);
        model.addAttribute("tt_travel_Type",tt_travel_Start);
        model.addAttribute("tt_travel_Type",tt_travel_End);

        return "/travelAgency/routes";
    }

    //查看线路详情
    @RequestMapping("/routeInfo")
    public String routeInfo(@RequestParam(required = false)String tt_travel_Id, Model model){
        t_travel travel=travelAgencyService.getXianlu(tt_travel_Id);
        model.addAttribute("travel",travel);
        return "/travelAgency/routeInfo";
    }

    //查看 交易页面
    @RequestMapping("/search")
    public String search(Model model,@RequestParam(required = false)String tt_transaction_Name,
                         @RequestParam(required = false)String tt_transaction_Type,@RequestParam(required = false)String tt_transaction_Time){

        if ("0".equals(tt_transaction_Type)){
            tt_transaction_Type=null;
        }
        List<t_transaction> travelList=travelAgencyService.getListSearch(tt_transaction_Name,tt_transaction_Type,tt_transaction_Time);
        model.addAttribute("tt_transaction",tt_transaction_Name);
        model.addAttribute("travelList",travelList);
        model.addAttribute("tt_transaction_Type",tt_transaction_Type);
        model.addAttribute("tt_transaction_Time",tt_transaction_Time);
        return "/travelAgency/tradingSearch";
    }

    //查看 投诉页面
    @RequestMapping("/complaint")
    public String complaint(Model model,@RequestParam(required = false)String tc_complaont_Name,
                            @RequestParam(required = false)String tc_complaont_Types,@RequestParam(required = false)String tc_complaont_Time){
        if ("0".equals(tc_complaont_Types)){
            tc_complaont_Types=null;
        }
        List<t_complaont> travelList=travelAgencyService.getListcomplaint(tc_complaont_Name,tc_complaont_Types,tc_complaont_Time);
        model.addAttribute("travelList",travelList);
        model.addAttribute("tc_complaont_Name",tc_complaont_Name);
        model.addAttribute("tc_complaont_Types",tc_complaont_Types);
        model.addAttribute("tc_complaont_Time",tc_complaont_Time);
        return "/travelAgency/complaintsSearch";
    }

    //添加
    @RequestMapping("/addHotel")
    public String addHotel(@ModelAttribute t_travel travel){
        return "/travelAgency/addHotel";
    }
    @RequestMapping("/add.do")
    public String add1(t_travel travel, HttpServletRequest request,
                       @RequestParam(name="myImage") MultipartFile path){

        System.out.println("添加信息");
        String filePath = null;
        //用来区分错误提示信息
        String errorInfo=null;
        // 先获取当前请求的全路径
        String p = request.getSession().getServletContext()
                .getRealPath("static" + File.separator + "uploadFile");
        System.out.println(p);
        // 解决用户上传的文件名和现在服务器上保存的
        // 文件名冲突的问题
        // 先获取旧的文件名
        String oldFileName = path.getOriginalFilename();
        System.out.println(oldFileName);
        // 获取旧文件名的后缀
        String prefix = FilenameUtils.getExtension(oldFileName);
        System.out.println(prefix);// png
        // 判断上传的文件大小是否超出规定
        if (path.getSize() > 50000000) {// 是否超过500k
            System.out.println("图片过大");
            return "/travelAgency/addHotel";
        }
        // 判断后缀是否符合要求
        if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
                || prefix.equalsIgnoreCase("gif")) {
            // 统一改名+后缀
            String newFileName =
                    // 当前系统的毫秒值+随机数+后缀
                    System.currentTimeMillis() + RandomUtils.nextInt(100000) + "." + prefix;
            // 使用File对象读取p路径下面的指定文件
            File file = new File(p, newFileName);
            if (!file.exists()) {
                // 创建多级目录
                file.mkdirs();
            }
            // 目录创建成功之后,将上传的文件保存起来
            try {
                path.transferTo(file);
            } catch (IllegalStateException | IOException e) {
                request.setAttribute(errorInfo, "上传失败");
                return "/travelAgency/addHotel";
                // e.printStackTrace();
            }
            // 给文件字段赋值
            filePath = p + File.separator + newFileName;
            travel.setTt_travel_Picture(filePath);
        }else{
            System.out.println("filePath："+filePath);
            return "/travelAgency/addHotel";
        }
        int num=  travelAgencyService.addTravel(travel);
        System.out.println("num："+num);

        return "redirect:/travelAgency/basic";
    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestParam(required = false)String tt_travel_Id){
        int num=  travelAgencyService.deleteTravel(tt_travel_Id);
        System.out.println("得到参数"+tt_travel_Id);
        System.out.println("删除判断"+num);
        return "true";
    }

    //修改
    @RequestMapping("/modifyHotel")
    public String xiugai(Model model,@ModelAttribute t_travel travel){
        int tt_travel_Id=travel.getTt_travel_Id();
        t_travel travel1 =travelAgencyService.getBasicView(String.valueOf(tt_travel_Id));
        model.addAttribute("travel1",travel1);
        return "travelAgency/modifyHotel";
    }
    @RequestMapping("/modify.do")
    @ResponseBody
    public String xiugai2(t_travel travel,HttpServletRequest request,
                          @RequestParam(name="myImage") MultipartFile path,String tt_travel_Createtime1){
        System.out.println("添加信息");
        String filePath = null;
        //用来区分错误提示信息
        String errorInfo=null;
        // 先获取当前请求的全路径
        String p = request.getSession().getServletContext()
                .getRealPath("static" + File.separator + "uploadFile");
        System.out.println(p);
        // 解决用户上传的文件名和现在服务器上保存的
        // 文件名冲突的问题
        // 先获取旧的文件名
        String oldFileName = path.getOriginalFilename();
        System.out.println(oldFileName);
        // 获取旧文件名的后缀
        String prefix = FilenameUtils.getExtension(oldFileName);
        System.out.println(prefix);// png
        // 判断上传的文件大小是否超出规定
        if (path.getSize() > 50000000) {// 是否超过500k
            System.out.println("图片过大");
            return "/travelAgency/modifyHotel";
        }
        // 判断后缀是否符合要求
        if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
                || prefix.equalsIgnoreCase("gif")) {
            // 统一改名+后缀
            String newFileName =
                    // 当前系统的毫秒值+随机数+后缀
                    System.currentTimeMillis() + RandomUtils.nextInt(100000) + "." + prefix;
            // 使用File对象读取p路径下面的指定文件
            File file = new File(p, newFileName);
            if (!file.exists()) {
                // 创建多级目录
                file.mkdirs();
            }
            // 目录创建成功之后,将上传的文件保存起来
            try {
                path.transferTo(file);
            } catch (IllegalStateException | IOException e) {
                request.setAttribute(errorInfo, "上传失败");
                return "/travelAgency/modifyHotel";
                // e.printStackTrace();
            }
            // 给文件字段赋值
            filePath = p + File.separator + newFileName;
            travel.setTt_travel_Picture(filePath);
        }else{
            System.out.println("filePath："+filePath);
            return "/travelAgency/modifyHotel";
        }
        SimpleDateFormat df =
                new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = df.parse(tt_travel_Createtime1);
            travel.setTt_travel_Createtime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int num=travelAgencyService.modify(travel);
        if (num>0){
            return  "true";
        }else {
            return "false";
        }
    }

    //查看 basic页面
    @RequestMapping("/viewHotel")
    public String view(@RequestParam(required = false)String tt_travel_Id,Model model){
        t_travel travel =travelAgencyService.getBasicView(tt_travel_Id);
        model.addAttribute("travel",travel);
        return "travelAgency/addHotel";
    }

}



